<template>
  <div class="equipment-container">
    <!-- 主要内容main -->
    <el-main v-show="true">
      <el-row :gutter="20">
        <!-- 左侧 -->
        <el-col :span="6" class="echarts_left">
          <el-card style="max-width: 480px" class="porduct">
            <template #header>
              <div class="card-header">
                <span>生产数据</span>
              </div>
            </template>
            <div
              class="card-body"
              ref="scaleRef"
              v-loading="fullscreenLoading"
              element-loading-background="rgba(122, 122, 122, 0.3)"
            >
              <div
                class="card-item"
                v-for="(item, index) in productList"
                :key="index"
              >
                <div class="item-box">
                  <img :src="item.url" alt="" />
                  <div class="box-text">
                    <div style="color: #409eff" class="text-value">
                      {{ item.value }}
                      <span>{{ item.unit }}</span>
                    </div>
                    <div style="color: #ccc;margin:10px 0px;">{{ item.title }}</div>
                  </div>
                </div>
              </div>
            </div>
          </el-card>

          <el-card style="max-width: 480px" class="envData">
            <template #header>
              <div class="card-header">
                <span>环境数据</span>
              </div>
            </template>
            <div
              class="card-body"
              ref="hkChartRef"
              style="height: 240px"
              v-loading="fullscreenLoading"
              element-loading-background="rgba(122, 122, 122, 0.3)"
            ></div>
            <div
              class="card-body"
              ref="scaleRef"
              style="height: 240px"
              v-loading="fullscreenLoading"
              element-loading-background="rgba(122, 122, 122, 0.3)"
            >
              <div
                class="card-item"
                v-for="(item, index) in envDataList"
                :key="index"
              >
                <div class="item-box">
                  <img :src="item.url" alt="" v-if="item.url"/>
                  <div class="box-text">
                    <div style="color: #409eff" class="text-value">
                      {{ item.value }}
                      <span>{{ item.unit }}</span>
                    </div>
                    <div style="color: #ccc;">{{ item.title }}</div>
                  </div>
                </div>
              </div>
            </div>
          </el-card>
        </el-col>
        <!-- 主视觉 -->
        <el-col :span="12" class="main_view">
          <div class="main-house">
            <el-select
              v-model="value"
              placeholder="请选择农场"
              style="width: 240px"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </div>
        </el-col>
        <!-- 右侧 -->
        <el-col :span="6" class="echarts_right">
           <el-card style="max-width: 480px">
            <template #header>
              <div class="card-header">
                <span>监控画面</span>
              </div>
            </template>
            <div class="card-body" ref="scaleRef" style="height: 240px">
              <!-- <ezuikit
                :accessToken="accessToken"
                :url="url"
                :key="componentKey"
                :template="'simple'"
              /> -->
              <img :src="monitor" style="width:95%;height:90%"/>  
            </div>
          </el-card>

          <el-card style="max-width: 480px" class="status">
            <template #header>
              <div class="card-header">
                <span>设备状态</span>
              </div>
            </template>
            <div
              class="card-body"
              ref="overviewRef"
              v-loading="fullscreenLoading"
              element-loading-background="rgba(122, 122, 122, 0.3)"
              style="height: 240px"
            >
              <div
                class="card-item"
                v-for="(item, index) in windDevicesList"
                :key="index"
              >
                <div class="item-box">
                  <img :src="item.imageUrl" class="device-img" :class="(item.status == 0 ?'gray-image': '')+(item.status ==1 && item.text.indexOf('风机') !== -1?'rotateImg':'') + ' image'" />
                  <div>{{ item.text }}</div>
                </div>
              </div>
            </div>
          </el-card>

          <el-card style="max-width: 480px" class="warn-info">
            <template #header>
              <div class="card-header">
                <span>告警信息</span>
              </div>
            </template>
            <div
              class="card-body"
              ref="waterChartRef"
              v-loading="fullscreenLoading"
              element-loading-background="rgba(122, 122, 122, 0.3)"
            >
              <el-table
                :data="faultList"
                style="width: 100%; height: 240px;"
                :header-row-style="{ height: '40px' }"
                :row-class-name="tableRowClassName"
                border 
              >
                <el-table-column prop="houseName" label="名称" width="80" />
                <el-table-column prop="faultMsg" label="告警信息" width="" />
                <el-table-column prop="startTime" label="告警时间" />
                <el-table-column prop="detail" label="持续时间" width="" align="center"/>
              </el-table>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </el-main>
  </div>
</template>

<script setup name="Equipment">
// import useDictStore from "@/store/modules/dict";
// import ezuikit from "../../../../components/Ezuikit";
// import { listFault, produce, env, status } from "../../../../api/daping/device";
import windLogo  from "@/assets/imgs/iot/风机组_on.png";
import warnLogo from "@/assets/imgs/iot/报警.png";
import penwuLogo from "@/assets/imgs/iot/喷雾.png";
import shuibeng from "@/assets/imgs/iot/水泵.png";
import windows from "@/assets/imgs/iot/通风窗.png";
import light from "@/assets/imgs/iot/光照.png";
import day from "@/assets/imgs/iot/日龄.png";
import windModel from "@/assets/imgs/iot/通风模式.png";
import windLevel from "@/assets/imgs/iot/通风等级.png";
import windSpeed from "@/assets/imgs/iot/通风量.png";
import temp from "@/assets/imgs/iot/目标温度.png";
import water from "@/assets/imgs/iot/用水量.png";
import consume from "@/assets/imgs/iot/料塔.png";
import outdoorTemp from "@/assets/imgs/iot/室外温度.png";
import innerdoorTemp from "@/assets/imgs/iot/舍内温度.png";
import Humidity  from "@/assets/imgs/iot/舍内湿度.png";
import carbon from "@/assets/imgs/iot/二氧化碳浓度.png";
import pressure from "@/assets/imgs/iot/负压.png";
import monitor from "@/assets/imgs/iot/monitor.png"
import * as echarts from "echarts";
import {
  onMounted,
  ref,
  watch,
  watchEffect,
  reactive,
  onUnmounted,
  computed,
} from "vue";

const router = useRouter();

/** getCurrentInstance使用注意点
 * getCurrentInstance 只能在 setup 或生命周期钩子中使用
 * getCurrentInstance线上环境报错问题: 使用proxy代替ctx,proxy线上不会出现问题
 * 在vue3中不推荐使用getCurrentInstance获取组件实例
 * 如果是获取dom大家可以通过ref获取
 * 至于其他的一些常用属性和方法，vue3中的setup中提供了props和contexts上下文
 */
const { proxy } = getCurrentInstance();
// const { access_token } = proxy.useDict("access_token");

const value = ref('广兴01舍')
const options = [
  {
    value: 'Option1',
    label: '广兴01舍',
  },
  {
    value: 'Option2',
    label: '广兴02舍',
  },
  {
    value: 'Option3',
    label: '广兴03舍',
  },
  {
    value: 'Option4',
    label: '广兴04舍',
  },
  {
    value: 'Option5',
    label: '广兴05舍',
  },
]


const deviceIdList = [
  "1B0F0CD8-1231-467F-A2AF-7BD64C8D7F35",
  "60F78AD9-8CA7-4C91-835F-1F691F39D314",
  "29D8A8B3-AC58-43E5-ABB7-6E7275C91CC4",
  "720BC377-E871-456D-BC6C-FC8528FB895C",
];

const deviceId = ref(deviceIdList[0]);

const urlList = [
  "ezopen://open.ys7.com/L06779548/32.hd.live",
  "ezopen://open.ys7.com/L06779548/12.hd.live",
  "ezopen://open.ys7.com/L06779548/13.hd.live",
  "ezopen://open.ys7.com/L06779548/27.hd.live",
];

const componentKey = ref(0);

// const accessToken = ref(
//   JSON.parse(localStorage.getItem("access_token"))[1].value
// );
const url = ref(urlList[0]);

const houseCurrent = ref("10#");

const fullscreenLoading = ref(false);

const hkChartRef = ref(null);
// 静态数据
const chartData = {
  xData: ["0点", "1点", "2点", "3点", "4点", "5点", "6点", "7点", "8点", "9点", "10点", "11点", "12点", "13点", "14点", "15点", "16点", "17点", "18点", "19点", "20点", "21点", "22点", "23点"],
  yData_outdoorTemp: [27, 26, 25, 24, 23, 22, 21, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 33, 32, 31], // 室外温度
  yData_indoorTemp: [35, 34, 34, 33, 32, 31, 30, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 40, 39, 38, 37], // 室内温度
  yData_humidity: [55, 60, 65, 70, 75, 80, 85, 90, 80, 75, 70, 65, 60, 55, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95], // 室内湿度
  yData_co2: [400, 410, 420, 430, 440, 450, 460, 470, 480, 490, 500, 510, 520, 530, 540, 550, 560, 570, 580, 590, 600, 610, 620, 630], // 二氧化碳浓度
};

/** 创建一个响应式变量currentTime，初始值为空字符串 */
//设备状态
const windDevicesList = ref([
    {
			imageUrl: windLogo,
			status: 1,
			text: '风机组1'
		},
    {
			imageUrl: windLogo,
			status: 0,
			text: '风机组2'
		},
    {
			imageUrl: windLogo,
			status: 1,
			text: '风机组3'
		},
    {
			imageUrl: windLogo,
			status: 0,
			text: '风机组4'
		},
    {
			imageUrl: windLogo,
			status: 1,
			text: '风机组5'
		},
    {
			imageUrl: windLogo,
			status: 0,
			text: '风机组6'
		},
    {
			imageUrl: windLogo,
			status: 1,
			text: '风机组7'
		},
    {
			imageUrl: windLogo,
			status: 1,
			text: '风机组8'
		},
    {
			imageUrl: windLogo,
			status: 1,
			text: '风机组9'
		},
    {
			imageUrl: windLogo,
			status: 0,
			text: '风机组10'
		},
    {
			imageUrl: shuibeng,
			status: 1,
			text: '水泵'
		},
    {
			imageUrl: penwuLogo,
			status: 1,
			text: '喷雾'
		},
    {
			imageUrl: warnLogo,
			status: 1,
			text: '警报'
		},
  ]);

const faultList = ref([
  {
    startTime: "2024-08-13 10:59:31.993",
    faultMsg: "温度过高",
    detail: '1min',
    deviceId: "dd",
    houseName: "GX001",
  },
  {
    startTime: "2024-08-13 10:59:31.993",
    faultMsg: "温度过高",
    detail: "10min",
    deviceId: "dd",
    houseName: "GX002",
  },
  {
    startTime: "2024-08-13 10:59:31.993",
    faultMsg: "温度过高",
    detail: "12min",
    deviceId: "dd",
    houseName: "GX003",
  },
  {
    startTime: "2024-08-13 10:59:31.993",
    faultMsg: "温度过高",
    detail: "4min",
    deviceId: "dd",
    houseName: "GX004",
  },
  {
    startTime: "2024-08-13 10:59:31.993",
    faultMsg: "二氧化碳浓度",
    detail: "6min",
    deviceId: "dd",
    houseName: "10#",
  },
  {
    startTime: "2024-08-13 10:59:31.993",
    faultMsg: "温度过低",
    detail: "6min",
    deviceId: "dd",
    houseName: "10#",
  },
]);

const data_24 = ref({});
// 生产数据
const productList = ref([
  {
    url: day,
    value: "167",
    title: "日龄(天)",
  },
  {
    url: windModel,
    value: "模式1",
    title: "通风模式",
  },
  {
    url: windLevel,
    value: "1",
    title: "通风级别",
  },
  {
    url: windSpeed,
    value: "82",
    title: "脉冲风速",
  },
  {
    url: temp,
    value: "35.0",
    title: "适宜温度(℃)",
  },
  {
    url: penwuLogo,
    value: "40.0",
    title: "喷雾温度(℃)",
  },
  {
    url: water,
    value: "40",
    title: "用水量(T)",
  },
  {
    url: consume,
    value: "5981",
    title: "料塔1(kg)",
  },
]);

const envDataList = ref([
  {
    url: outdoorTemp,
    value: "27.5",
    title: "室外温度",
  },
  {
    url: innerdoorTemp,
    value: "35.5",
    title: "舍内温度",
  },
  {
    url:Humidity,
    value: "59",
    title: "舍内湿度",
  },
  {
    url: carbon,
    value: "1100",
    title: "CO2浓度",
  },
  {
    url: pressure,
    value: "4.3", 
    title: "负压",
  },
  {
    value: "4.3",
    title: "主板温度1",
  },
  {
    value: "4.3",
    title: "主板温度2",
  },
  {
    value: "4.3",
    title: "主板温度3",
  },
  {
    value: "4.3",
    title: "主板温度4",
  },
  {
    value: "4.3",
    title: "从板温度1",
  },
  {
    value: "4.3",
    title: "从板温度2",
  },
  {
    value: "4.3",
    title: "从板温度3",
  },
  {
    value: "4.3",
    title: "从板温度4",
  },
]);

const handleMore = () => {
  // const targetPath = "/" + key;
  // // 判断当前的路径是否重复
  // if (router.path === targetPath) {
  //   // 如果相同就不进行导航
  //   return;
  // }
  // 否则进行导航
  router.push({ path: "/cockpit/inspect" });
};

const handleCommand = (command) => {
  houseCurrent.value = command;
};

// 初始化图表的函数
const initChart = (chartRef, data) => {
  const chartInstance = echarts.init(chartRef.value);
  const option = {
    legend: {
      data: ["室外温度", "室内温度", "室内湿度", "二氧化碳浓度"],
      left: "center",
      textStyle: {
        color: "#fff",
      },
    },
    grid: {
      top: "15%",
      left: "3%",
      right: "4%",
      bottom: "3%",
      containLabel: true,
    },
    tooltip: {
      trigger: "axis",
    },
    xAxis: {
      type: "category",
      data: data.xData,
      axisLine: {
        show: false,
        lineStyle: {
          color: "#fff",
        },
      },
    },
    yAxis: [
      {
        type: "value",
        axisLine: {
          show: false,
        },
        axisLabel: {
          color: "#FFF",
          fontSize: 12,
        },
        splitLine: {
          show: true,
          lineStyle: {
            type: "dotted",
            color: "rgba(255, 255, 255,0.3)",
          },
        },
      },
    ],
    series: [
      {
        name: "室外温度",
        data: data.yData_outdoorTemp,
        type: "line",
        smooth: true,
        symbol: "none",
      },
      {
        name: "室内温度",
        data: data.yData_indoorTemp,
        type: "line",
        smooth: true,
        symbol: "none",
      },
      {
        name: "室内湿度",
        data: data.yData_humidity,
        type: "line",
        smooth: true,
        symbol: "none",
      },
      {
        name: "二氧化碳浓度",
        data: data.yData_co2,
        type: "line",
        smooth: true,
        symbol: "none",
      },
    ],
  };
  
  chartInstance.setOption(option);
  window.addEventListener("resize", () => {
    chartInstance.resize();
  });
};
const handleClick = (tab, event) => {
  console.log(tab, event);
};

const data = reactive({
  weatherList: {},
});

const { weatherList } = toRefs(data);

const getFault = async (deviceId) => {

};

const getEnv = async (deviceId) => {

};

const getStatus = async (deviceId) => {
  
};

const getList = async (deviceId) => {
  fullscreenLoading.value = true;
  // await getFault(deviceId);
  // await getProduce(deviceId);
  // await getEnv(deviceId);
  // await getStatus(deviceId);

  // const hkChartInstance = echarts.init(hkChartRef.value);
  // updateChartData(data_24.value, hkChartInstance);
  // window.addEventListener("resize", () => {
  //   hkChartInstance.resize();
  // });
  fullscreenLoading.value = false;
};

onMounted(() => {
  // getList(deviceId.value);
  initChart(hkChartRef, chartData); // 调用初始化图表的函数
});

// 使用 watchEffect 来响应数据的变化
watchEffect(() => {});
</script>

<style scope lang="scss">
.equipment-container {
  height: 87%;
  width: 100%;
  .el-main {
    width: 100%;
    height: 100%;
    overflow-x: hidden;
    // background-color: yellow;
    padding: 15px;
    .el-row {
      height: 100%;
    }
    .el-card {
      color: #fff;
      // height: 100%;
      background: linear-gradient(
        180deg,
        #001e54 0%,
        rgba(4, 51, 132, 0.29) 100%
      );
      .el-card__header {
        padding: 0 !important;
        min-height: 28px;
        .card-header {
          font-size: 20px;
          color: #1890FF;
          min-height: 36px;
          padding: 0 0 0 16px;
          display: flex;
          align-items: center;
        }
      }
      .el-card__body {
        padding: 0 !important;
        // height: calc(100% - 28px);
        min-height: calc(100% - 28px);
        .card-body {
          height: 100%;
          display: flex;
          justify-content: space-evenly;
          align-items: center;
          .status-item {
            height: 280px;
            display: flex;
            flex-direction: column;
            justify-content: space-evenly;
            align-items: center;
          }
          .statusRing {
            width: 100px;
            height: 100px;
            // line-height: 50px;
            background: url("../../../../assets/images/ring.png") no-repeat
              center;
            background-size: 100% 100%;
            text-align: center;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
          }
          .statusInfo {
            padding: 10px 4px;
            border-radius: 6px;
            background-color: #001e54;
            display: flex;
            flex-direction: column;
            width: 100px;
            gap: 10px;
            .info-item {
              display: flex;
              justify-content: space-evenly;
              align-items: center;
              i,
              span {
                width: 33.33%;
                text-align: center;
                font-size: 14px;
              }
            }
          }
        }
      }
    }
    .echarts_left {
      display: flex;
      flex-direction: column;
      justify-content: space-evenly;
      .more {
        // color: #001e54;
        cursor: pointer;
        margin-left: 300px;
      }
    }
    .main_view {
      margin-top: 10px;
      .main-house{
        background-color: skyblue;
        margin: 0 40%;
      }
      .cont-list {
        display: flex;
        background: rgba(36, 54, 111, 0.9);
        box-shadow: 0 4px 10px #0000001d;
        justify-content: space-around;
        align-items: center;
        padding: 10px;
        position: relative;
        .cont-item {
          display: flex;
          .cont-item-left {
            display: flex;
            flex-direction: column;
            justify-content: center;
            img {
              width: 44px;
              height: 44px;
              object-fit: contain;
            }
            span {
              font-size: 18px;
              font-weight: 500;
              color: #fff;
              line-height: 25px;
            }
          }
          .cont-item-right {
            padding-left: 20px;
            .day {
              font-size: 22px;
              font-weight: 700;
              color: #fff;
              line-height: 26px;
            }
            .data {
              font-size: 32px;
              font-weight: 700;
              color: #fff;
              line-height: 33px;
              padding-top: 10px;
            }
            .unit {
              font-size: 22px;
              font-weight: 600;
              color: #b6c1c4;
              line-height: 26px;
              padding-left: 10px;
            }
          }
        }
      }
    }
    .echarts_right {
      display: flex;
      flex-direction: column;
      justify-content: space-evenly;
    }
  }
}

.status .card-body,
.porduct .card-body,
.envData .card-body {
  padding: 12px 0px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-evenly;
  align-items: center;
  text-align: center;
  .card-item {
    width: 25%;
  }
  
}

.status .card-body{
  .card-item {  
    width: 20%;
    .item-box{
      .gray-image {
        filter: grayscale(100%);
        /* 设置灰度值 */
      }
      .rotateImg{
        animation: rotate 2s linear infinite;
      }
      .device-img{
        width: 40px;
        height: 40px;
      }
    }
  }
}

.porduct .card-body{
  .card-item {
    .item-box {
      padding: 5px;
      img{
        height: 50px;
        width: 50px;
      }
    }
  }
}

.envData .card-body {
  .card-item {
    width: 20%;
    .item-box {
      padding: 5px;
      img{
        height: 40px;
        width: 40px;
      }
    }
  }
}
// 告警信息
.warn-info .card-body {
  padding: 10px;
  .el-table__header-wrapper{
    background-color: rgb(82, 172, 255);
    color: #fff;
  }
}

.card-body {
  .el-table {
    --el-table-border-color: transparent;
    --el-table-border: none;
    --el-table-text-color: #bdbdbe;
    --el-table-header-text-color: #ffffff;
    --el-table-row-hover-bg-color: skyblue;
    --el-table-current-row-bg-color: transparent;
    --el-table-header-bg-color: transparent;
    --el-table-bg-color: transparent;
    --el-table-tr-bg-color: transparent;
    --el-table-expanded-cell-bg-color: transparent;
    color: #ffffff;
    font-size: 13px;
  }
}

.el-table .el-table__header-wrapper th,
.el-table .el-table__fixed-header-wrapper th {
  // background-color: transparent!important;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.rotate {
  animation: rotate 3s linear infinite;
}
</style>
